package com.company.cloud.product.client;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.company.cloud.product.modules.base.entity.ProductStockInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.product.modules.api.request.ProductStockMultipleSkuOpParam;
import com.company.cloud.product.modules.api.request.ProductStockSingleSkuOpParam;
import com.company.cloud.product.modules.api.response.PrdSkuStock;
import com.company.cloud.product.modules.api.service.ProductStoctRemoteService;
import com.company.cloud.product.modules.app.service.ProductStockQueryService;
import com.company.cloud.product.modules.base.entity.ProductStockInfo;
import com.company.cloud.product.modules.mgr.service.ProductStockMgrService;

@RestController
@Service
public class ProductStoctRemoteServiceImpl implements ProductStoctRemoteService {

	@Autowired
	private ProductStockMgrService productStockMgrService;
	@Autowired
	private ProductStockQueryService productStockQueryService;

	@Override
	public BaseClientResult<String> opSingleSkuStock(ProductStockSingleSkuOpParam params) {
		try {
			ValidatorUtils.validate(params);
			productStockMgrService.opStock(params);
			return BaseClientResult.success();
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseClientResult<String> opMultipleSkuStock(ProductStockMultipleSkuOpParam params) {
		try {
			ValidatorUtils.validate(params);
			productStockMgrService.opStock(params);
			return BaseClientResult.success();
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	private PrdSkuStock getPrdSkuStock(ProductStockInfo entity) {
		PrdSkuStock data = new PrdSkuStock();
		data.setProductNo(entity.getProductNo());
		data.setSkuNo(entity.getSkuNo());
		data.setNum(entity.getNum());
		data.setFreezeNum(entity.getFreezeNum());
		return data;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseClientResult<Map<String, PrdSkuStock>> getSkuStockList(Set<String> skuNos) {
		Map<String, PrdSkuStock> data = new HashMap<String, PrdSkuStock>();
		if (CollectionUtils.isEmpty(skuNos)) {
			return BaseClientResult.success(data);
		}
		try {
			Map<String, ProductStockInfo> stockMap = productStockQueryService.getSkuStockBySkuNo(skuNos);
			if (!CollectionUtils.isEmpty(stockMap)) {
				stockMap.forEach((key, val) -> {
					data.put(key, this.getPrdSkuStock(val));
				});
			}
			return BaseClientResult.success(data);
		} catch (Exception e) {
			e.printStackTrace();
			return BaseClientResult.error(e);
		}

	}

	@Override
	public BaseClientResult<Map<String, PrdSkuStock>> getPrdStockList(Set<String> prdNos) {
		Map<String, PrdSkuStock> data = new HashMap<String, PrdSkuStock>();
		if (CollectionUtils.isEmpty(prdNos)) {
			return BaseClientResult.success(data);
		}
		try {
			Map<String, ProductStockInfo> stockMap = productStockQueryService.getPrdStockByPrd(prdNos);
			if (!CollectionUtils.isEmpty(stockMap)) {
				stockMap.forEach((key, val) -> {
					data.put(key, this.getPrdSkuStock(val));
				});
			}
			return BaseClientResult.success(data);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
		
	}

	@Override
	public BaseClientResult<Map<String, PrdSkuStock>> getPrd2SkuStock(String prdNo) {
		Map<String, PrdSkuStock> data = new HashMap<String, PrdSkuStock>();
		if (StringUtils.isEmpty(prdNo)) {
			return BaseClientResult.success(data);
		}
		try {
			Map<String, ProductStockInfo> stockMap = productStockQueryService.getSkuStockByPrdNo(prdNo);
			if (!CollectionUtils.isEmpty(stockMap)) {
				stockMap.forEach((key, val) -> {
					data.put(key, this.getPrdSkuStock(val));
				});
			}
			return BaseClientResult.success(data);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

}
